var util = require('../../../../utils/util.js');
var api = require('../../../../config/api.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    orderInfo: {
      type: Object,
      value: ''
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    labelArr: [{
      value: "青春阳光",
      select: false
    }, {
      value: "时尚",
      select: false
    }, {
      value: "美丽大方",
      select: false
    }, {
      value: "超值",
      select: false
    }, {
      value: "思维逻辑强",
      select: false
    }, {
      value: "具有亲和力",
      select: false
    }, {
      value: "非常有耐心",
      select: false
    }, {
      value: "体验极佳",
      select: false
    }],
    rateCommnet: "", //评价
    label: "", //自定义标签
    more: false,
    start: 0, //评分
    startTXT: "未评价"
  },

  /**
   * 组件的方法列表
   */
  methods: {
    commit: function() {
      // var myEventDetail = {
      //   msg: '来自component的信息'
      // }
      // var myEventOption = {}
      // this.triggerEvent('myeventB', myEventDetail, myEventOption)
      //提交评价  
      let k = 0;
      for (let item of this.data.labelArr) {
        if (item.select == true) {
          k = ++k
        }
      }
      //判断评价标签选三个
      if (k > 3) {
        wx.showToast({
          title: `只能选择三个评价标签`,
          icon: 'none'
        });
        return false
      }
      //评论必填
      if (this.data.rateCommnet == "") {
        wx.showToast({
          title: `评论信息必填`,
          icon: 'none'
        });
        return false
      }
      k = 0;
      //过滤表情
      let rateCommnet = this.filterEmoji(this.data.rateCommnet)
      let userComment = {
        content: rateCommnet,
        userId: wx.getStorageSync('userInfo').id,
        userOrderId: this.properties.orderInfo.id,
        commentLabels: [],
        star: parseInt(this.data.start),
        tutorId: this.properties.orderInfo.to_user_id
      }
      this.data.labelArr.forEach((value, index, arr) => {
        if (value.select) {
          let comment = {
            labelName: value.value
          }
          userComment.commentLabels.push(comment)
        }
      })
      console.log(userComment);
      util.request(api.SaveUserComment, 
        userComment
      , "POST", true).then(function(res) {
        if (res.code === 200) {
          wx.showModal({
            content: '您已成功完成订单，点击确定返回首页',
            success(res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '/pages/index/index',
                });
              } else if (res.cancel) {
                wx.switchTab({
                  url: '/pages/index/index',
                });
              }
            },
            showCancel: false
          })
        }else{
          wx.showToast({
            title: res.message,
            icon: 'none'
          });
        }
      });
    },
    selectLabel: function(e) {
      this.data.labelArr[e.target.dataset.index].select = !this.data.labelArr[e.target.dataset.index].select
      this.setData({
        labelArr: this.data.labelArr
      })
    },
    comfirmLabel: function(e) {
      let newLabelArr = this.data.labelArr;
      newLabelArr.push({
        value: this.data.label,
        select: false
      })
      this.setData({
        more: false,
        labelArr: newLabelArr
      })
    },
    moreLabel: function(e) {
      this.setData({
        more: true
      })
    },
    labelInput: function(e) {
      this.setData({
        label: e.detail.value
      })
    },
    rateCommentInput: function(e) {
      this.setData({
        rateCommnet: e.detail.value
      })
    },
    //匹配表情
    filterEmoji(name) {
      var str = name.replace(/[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig, "(不支持表情)");
      return str;
    },
    onChange(e) {
      let txt = "";
      switch (e.detail) {
        case 5:
          txt = "非常满意"
          break;
        case 4:
          txt = "非常满意"
          break;
        case 3:
          txt = "基本满意"
          break;
        case 2:
          txt = "基本满意"
          break;
        case 1:
          txt = "不满意"
          break;
        default:
          txt = "满意"
      }
      this.setData({
        startTXT: txt,
        start: e.detail
      })
    }
  },

  ready: function() {}
})